package vip.mtdp.mybatis;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * @description 数据源配置
 * @date 2020/3/28 17:45
 * @auth wangguoqing
 **/
@Slf4j
@Data
@Configuration
@MapperScan(basePackages = "vip.mtdp.**.dal")
@ConfigurationProperties(prefix = "mybatis")
@EnableTransactionManagement
public class DataSourceConfiguration {

    private String url;

    private String username;

    private String password;

    private String driverClassName;

    private int initialSize;

    private int minIdle;

    private int maxActive;

    private int maxWait;

    private int timeBetweenEvictionRunsMillis;

    private int minEvictableIdleTimeMillis;

    private String validationQuery;

    private boolean testWhileIdle;

    private boolean testOnBorrow;

    private boolean testOnReturn;

    private String filters;

    private String logSlowSql;

    private String connectionProperties;

   @Bean("createDataSource")
   @ConfigurationProperties(prefix = "mybatis")
   public DataSource createDataSource(){
       DruidDataSource ds = new DruidDataSource();
       ds.setUrl(url);
       ds.setUsername(username);
       ds.setPassword(password);
       ds.setDriverClassName(driverClassName);
       ds.setInitialSize(initialSize);
       ds.setMinIdle(minIdle);
       ds.setMaxActive(maxActive);
       ds.setMaxWait(maxWait);
       ds.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
       ds.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
       ds.setValidationQuery(validationQuery);
       ds.setTestWhileIdle(testWhileIdle);
       ds.setTestOnBorrow(testOnBorrow);
       ds.setTestOnReturn(testOnReturn);
       ds.setConnectionProperties(connectionProperties);
       log.info("=====> ds = {}",ds);
       return ds;
   }

//    @Bean("sqlSessionFactoryBean")
//    public SqlSessionFactoryBean createSqlSessionFactory(@Autowired DataSource dataSource) throws IOException {
//        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//
//        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//        sqlSessionFactoryBean.setDataSource(dataSource);
//        log.info("=====> mapper scan start");
//        Resource[] allResource = resolver.getResources("classpath*:/vip/mtdp/**/*.xml");
//        sqlSessionFactoryBean.setMapperLocations(allResource);
//        sqlSessionFactoryBean.setConfigLocation(resolver.getResource("classpath*:/config/mybatis-config.xml"));
//        log.info("=====> mapper scan end");
//        return sqlSessionFactoryBean;
//    }

}
